<template>
	<view class="container bg-gradual-blue">
		<view class="title text-xxl">天能无忧服务</view>
		<view class="ctrBtnContainer">
			<view class="btnPosition" @click="agentLogin">
				<view class="iconChange">
					<u-icon name="people" custom-prefix="cuIcon" color="#fef1c5" size="60"></u-icon>
				</view>
				<view class="iconBottom" style="animation-delay: 1s;">我是老板</view>
			</view>
			<view class="btnPosition" @click="agentStaffLogin">
				<view class="iconChange">
					<u-icon name="friendfavor" custom-prefix="cuIcon" color="#fef1c5" size="60"></u-icon>
				</view>
				<view class="iconBottom" style="animation-delay: 1.2s;">我是员工</view>
			</view>
			<view class="btnPosition" @click="accountLoginClick">
				<view class="iconChange">
					<u-icon name="refresh" custom-prefix="cuIcon" color="#fef1c5" size="60"></u-icon>
				</view>
				<view class="iconBottom" style="animation-delay: 1.4s;" >测试登录</view>
					
			</view>
			<view class="btnPosition" @click="programLoginClick">
				<view class="iconChange">
					<u-icon name="apps" custom-prefix="cuIcon" color="#fef1c5" size="60"></u-icon>
				</view>
				<view class="iconBottom" style="animation-delay: 1.4s;" >工程登录</view>	
			</view>

			<!-- <button class="cu-btn bg-yellow shadow animation-slide-bottom" style="animation-delay: 1.6s;" @click="wxAgentLoginClick">代理商</button> -->
		</view>
	</view>
</template>

<script>
import { mapActions, mapMutations } from 'vuex';
import subMessageIds from '@/config/subMessageIds.js';
import { getUserInfo } from '@/utils/tools.js';

export default {
	data() {
		return {
			canOpe: true
		};
	},
	computed: {
		openid() {
			return this.$store.state.user.userInfo.openId;
		}
	},
	onLoad() {
		console.log('onLoad');
	},
	onReady() {
		wx.showShareMenu({
			withShareTicket: true,
			menus: ['shareAppMessage']
		});
	},
	methods: {
		...mapActions({
			serverWXLogin: 'user/serverWXLogin',
			agentServerLogin: 'agent/agentServerLogin',
			code2session: 'mp/code2session',
			agentStaffServerLogin: 'agent/agentStaffServerLogin',
			getServerUserInfo: 'user/getServerUserInfo'
		}),
		...mapMutations({
			setUserState: 'user/setUserState'
		}),
		test() {
			wx.openSetting({
				withSubscriptions: true,
				success(res) {
					console.log('setting', res);
				},
				fail(err) {
					console.log('setting', err);
				}
			});
		},
		agentLogin() {
			let self = this;
			if (this.canOpe) {
				let storageUserInfo = getUserInfo();
				if (storageUserInfo.openId) {
					uni.showLoading({
						title: '请求中',
						mask: true
					});
					self.agentServerLogin({
						wxAppOpenId: storageUserInfo.openId
					})
						.then(res => {
							console.log(res);
							if (res) {
								uni.hideLoading();
								switch (res.code) {
									case '000000': {
										// 登录成功

										self.getInfoFromServer(res1 => {
											uni.navigateTo({
												url: '/agentPages/agent/agentIndex?type=agent'
											});
										});
										break;
									}
									case '000002': {
										// 手机号未绑定公众号
										uni.showModal({
											title: '登录失败',
											// content: '请前往公众号-产品服务-安装售后-微信手机绑定进行手机号绑定',
											content: '请进行公众号和手机号的绑定',
											showCancel: false,
											success: function(res) {
												if (res.confirm) {
													console.log('用户点击确定');
													uni.navigateTo({
														url: `/agentPages/agent/agentBindPhoneWeb`
													});
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										});
										break;
									}
									case '000001': {
										// 新用户
										uni.showModal({
											title: '登录失败',
											content: '您尚未注册，请前往注册',
											confirmText: '去注册',
											success: function(res) {
												if (res.confirm) {
													console.log('用户点击确定');
													uni.navigateTo({
														url: `/pages/login/reg?t=agent`
													});
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										});
										break;
									}
									default: {
										uni.showModal({
											title: '登录失败',
											content: res.msg,
											confirmText: '确定',
											showCancel: false,
											success: function(res) {
												if (res.confirm) {
													console.log('用户点击确定');
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										});
									}
								}
							}
						})
						.catch(err => {
							console.log(err);
							uni.hideLoading();
							uni.showToast({
								title: '登录失败：网络错误',
								icon: 'none'
							});
						});
				} else {
					uni.showToast({
						title: '登录失败：获取openId失败',
						icon: 'none'
					});
				}
			}
		},
		agentStaffLogin() {
			let self = this;
			if (this.canOpe) {
				let storageUserInfo = getUserInfo();
				if (storageUserInfo.openId) {
					uni.showLoading({
						title: '请求中',
						mask: true
					});
					self.agentStaffServerLogin({
						wxAppOpenId: storageUserInfo.openId
					})
						.then(res => {
							console.log(res);
							if (res) {
								uni.hideLoading();
								switch (res.code) {
									case '000000': {
										// 登录成功

										self.getInfoFromServer(() => {
											uni.navigateTo({
												url: '/agentPages/agent/agentIndex?type=staff'
											});
										});
										break;
									}
									case '000002': {
										// 手机号未绑定公众号
										uni.showModal({
											title: '登录失败',
											// content: '请前往公众号-产品服务-安装售后-微信手机绑定进行手机号绑定',
											content: '请进行公众号和手机号的绑定',
											showCancel: false,
											success: function(res) {
												if (res.confirm) {
													console.log('用户点击确定');
													uni.navigateTo({
														url: `/agentPages/agent/agentBindPhoneWeb?type=agentStaff`
													});
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										});
										break;
									}
									case '000001': {
										// 新用户
										uni.showModal({
											title: '登录失败',
											content: '您尚未注册，请前往注册',
											confirmText: '去注册',
											success: function(res) {
												if (res.confirm) {
													console.log('用户点击确定');
													uni.navigateTo({
														url: `/pages/login/reg?t=as`
													});
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										});
										break;
									}
									default: {
										uni.showModal({
											title: '登录失败',
											content: res.msg,
											confirmText: '确定',
											showCancel: false,
											success: function(res) {
												if (res.confirm) {
													console.log('用户点击确定');
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										});
									}
								}
							}
						})
						.catch(err => {
							console.log('err', err);
							uni.hideLoading();
							uni.showToast({
								title: '登录失败',
								icon: 'none'
							});
						});
				} else {
					uni.showToast({
						title: '登录失败：获取openId失败',
						icon: 'none'
					});
				}
			}
		},
		accountLoginClick() {
			uni.navigateTo({
				url: '../login/login?type=agentTest'
			});
		},
		programLoginClick(){
			console.log(this.$store.state.user.userInfo.openId);
			let src='test'+'#';
			// let url='http://localhost:8888/#/?wxAppOpenId='+src;
			let url='https://app.aptenon.com/crm/projectManagement/index.html?wxAppOpenId='+src+'/wxlogin';
			// wx.setStorageSync('url',encodeURIComponent(url));
			wx.setStorageSync('url',url);
			console.log(url);
			uni.navigateTo({
				url:'../login/programLogin'
			});
		},
		servicerLoginClick() {
			uni.navigateTo({
				url: '../login/login?type=servicer'
			});
		},
		getInfoFromServer(callback) {
			let storageUserInfo = getUserInfo();
			let openId = storageUserInfo.openId;
			uni.showLoading({
				title: '请求中',
				mask: true
			});
			this.getServerUserInfo({
				wxAppOpenId: openId
			})
				.then(res => {
					console.log(res);
					uni.hideLoading();
					if (res && res.code == '000000') {
						this.setUserState({
							...res.data,
							openId: openId
						});
					}
					if (callback) {
						callback(res.data);
					}
				})
				.catch(err => {
					uni.hideLoading();
					console.log(err);
				});
		},
		wxAgentLoginClick() {
			uni.navigateTo({
				url: `/agentPages/agent/wxAgentAuth`
			});
		}
	}
};
</script>

<style lang="less">
@import '../../common/var.less';
.container {
	height: 100vh;
	// background-image: linear-gradient(@primary-color, black);
	// background-image: url(../../static/bg1.jpg);
	background-image: url(http://app.aptenon.com/crm/miniprogram_pic/bg1.jpg);
	.title {
		padding-top: 100rpx;
		text-align: center;
		color: white;
		// background-color: black;
	}
	.ctrBtnContainer {
		width: 100%;
		display: flex;
		flex-wrap: nowrap;
		justify-content: space-between;
		position: fixed;
		padding: 0 50rpx;
		bottom: 200rpx;
		.btnPosition{
			// background-color: blue;
			color: #fff;
			width: 33.33%rpx;
			height: 140rpx;
			display: flex;
			flex-flow:column;
			justify-content: space-between;
			font-size: 50rpx;
			.iconChange{
				height: 70rpx;
				width: 33.33%rpx;
				// background-color: #fff;
				u-icon{
					height: 70rpx;
					width: 100%rpx;
					size:50;
					margin-left:26rpx;
				}
			}
			.iconBottom{
				height: 60rpx;
				// background-color: pink;
			}
			
		}
			
		// button {
		// 	height: 80rpx;
		// 	font-size: 32rpx;
		// 	width: 300rpx;
		// 	margin-top: 24rpx;
		// }
	}
}
</style>
